/*
 * @Author: zu1662
 * @LastEditor: zu1662
 * @Date: 2019-12-27 10:33:01
 * @LastEditTime: 2021-07-27 14:59:28
 * @Description: filters
 */
import Vue from 'vue'
import moment from 'moment'
import 'moment/locale/zh-cn'
moment.locale('zh-cn')

/**
 * like 2 days ago
 * 如果超出时间跨度，显示 时间格式
 * @param {date|string|number} time
 * @param {number} daySpan
 * @return {string}
 */
function timeAgo (time, daySpan = 5) {
  const getTime = moment(time).unix()
  const nowTime = moment().unix()
  const daySet = daySpan * 24 * 60 * 60
  if (nowTime - getTime > daySet) {
    return moment(time).format('YYYY-MM-DD HH:mm:ss')
  } else {
    return moment(time).startOf('second').fromNow()
  }
}
Vue.filter('timeAgo', timeAgo)
/**
 * dateFormat
 * @param {date|string|number} time
 * @param {string} formatter
 * @return {string}
 */
function dateFormat (time, formatter = 'YYYY-MM-DD HH:mm:ss') {
  return moment(time).format(formatter)
}
Vue.filter('dateFormat', dateFormat)

/**
 * 10000 => "10,000"
 * @param {number} num
 */
function toThousandFilter (num) {
  return (+num || 0).toString().replace(/^-?\d+/g, m => m.replace(/(?=(?!\b)(\d{3})+$)/g, ','))
}
Vue.filter('toThousandFilter', toThousandFilter)

/**
 * 数字后面有浮点显示
 * @param {number} n
 * @returns
 */
export function NumFormat (n) {
  if (!n) return '0.00'
  n = parseFloat(n).toFixed(2)
  const numStr = n.toString()
  const b = parseInt(n).toString()
  let decimalNum = ''
  if (n % 1 !== 0) {
    decimalNum = '.' + numStr.replace(/\d+\.(\d*)/, '$1')
  } else {
    decimalNum = '.00'
  }
  const len = b.length
  if (len <= 3) {
    return b + decimalNum
  }
  const r = len % 3
  const intNum = r > 0 ? b.slice(0, r) + ',' + b.slice(r, len).match(/\d{3}/g).join(',') : b.slice(r, len).match(/\d{3}/g).join(',')
  return intNum + decimalNum
}
Vue.filter('numFormat', NumFormat)

/**
 * 首字母大写
 * @param {String} string
 */
function uppercaseFirst (string) {
  return string.charAt(0).toUpperCase() + string.slice(1)
}
Vue.filter('uppercaseFirst', uppercaseFirst)

// 时间戳转换 通过flag识别转换为时间或者日期 （'date','time'）
// 使用element table组件中的formatter属性，传入一个函数
export function timestampToChange (timestamp, flag) {
  // 时间戳为10位需*1000，时间戳为13位的话不需乘1000
  if (!timestamp) {
    return ''
  }
  var date = new Date(parseInt(timestamp))
  var Y = date.getFullYear() + '-'
  var M = (date.getMonth() + 1).toString().padStart(2, '0') + '-'
  var D = date.getDate().toString().padStart(2, '0') + ' '
  if (flag === 'time') {
    var h = date.getHours().toString().padStart(2, '0') + ':'
    var m = date.getMinutes().toString().padStart(2, '0') + ':'
    var s = date.getSeconds().toString().padStart(2, '0')
    return Y + M + D + h + m + s
  } else if (flag === 'timer') {
    var hh = date.getHours().toString().padStart(2, '0') + ':'
    var mm = date.getMinutes().toString().padStart(2, '0') + ':'
    var ss = date.getSeconds().toString().padStart(2, '0')
    return hh + mm + ss
  } else {
    return Y + M + D
  }
}
Vue.filter('timestampToChange', timestampToChange)

export function changeToNA (data) {
  if (!data) return 'N/A'
  return data
}
Vue.filter('changeToNA', changeToNA)
